﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI;
using JQuery.Study.Dominio;

namespace JQuery.Study.WebFormSite.Controls
{
    public partial class SubMenu : UserControl
    {
        public string Section { set; get; }

        protected void Page_Load(object sender, EventArgs e)
        {
            SetSubmenu();
        }

        private void SetSubmenu()
        {
            if (Section == "Ajax")
                _submenu.InnerHtml = SetAjaxSubMenu();
        }

        private string SetSubMenuHtml(IEnumerable<Url> urls)
        {
            var submenu = new StringBuilder();

            foreach (var url in urls)
            {
                submenu.AppendFormat("<li><a href='{0}'>{1}</a></li>", url.File, url.Label);

                if (url.Child == null) 
                    continue;

                submenu.AppendLine("<li>");
                submenu.AppendLine("    <ul>");

                submenu.Append(SetSubMenuHtml(url.Child));

                submenu.AppendLine("    </ul>");
                submenu.AppendLine("</li>");
            }

            return submenu.ToString();
        }

        private string SetAjaxSubMenu()
        {
            var urls = new List<Url>
                           {
                             new Url
                                 {
                                     Label = "Web Service",
                                     Child = new List<Url>
                                     {
                                         new Url { File = "web-service-post.aspx", Label = "Post" },
                                         new Url { File = "web-service-get.aspx", Label = "Get" }
                                     }
                                 },
                             new Url
                                {
                                    Label = "Web Method",
                                    Child = new List<Url>
                                    {
                                        new Url { File = "web-method-post.aspx", Label = "Post" },
                                        new Url { File = "web-method-get.aspx", Label = "Get" }
                                    }
                                }
                                , new Url
                                     {
                                         Label = "Generic Handler",
                                         Child = new List<Url>
                                         {
                                             new Url { File = "generic-handler-post.aspx", Label = "Post" },
                                             new Url { File = "generic-handler-get.aspx", Label = "Get" }
                                         }
                                     }
                           };

            return SetSubMenuHtml(urls);
        }
    }
}